package protocol;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import play.i18n.Messages;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装json请求
 * 
 */
public class JsonRequest {

    /**
     * 请求的服务方法
     */
    public String service;
    /**
     * 请求ID
     */
    public String id;
    /**
     * 请求客户端
     */
    public Client client;
    /**
     * 请求参数
     */
    public JsonObject data;
    /**
     * 签名算法
     */
    public String encrypt;
    /**
     * 请求签名
     */
    public String sign;

    public static class Client {

        public String caller;
        public String ex;
    }

    private void throwParamTypeError(String paramName, String type) {
        throw new IllegalParamException(Messages.get("param_illegal", paramName, type));
    }

    public String getString(String key) {
        if(key == null){
            return null;
        }
        if (data != null && data.has(key)) {
            JsonElement value = data.get(key);
            if (value.isJsonNull()) {
                return null;
            }
            return value.getAsString();
        }
        return null;
    }

    public Long getLong(String key) {
        if(key == null){
            return null;
        }
        if (data != null && data.has(key)) {
            try {
                JsonElement value = data.get(key);
                if (value.isJsonNull()) {
                    return null;
                }
                return value.getAsLong();
            } catch (Exception e) {
                throwParamTypeError(key, "Long");
            }
        }
        return null;
    }

    public Integer getInteger(String key) {
        if(key == null){
            return null;
        }
        if (data != null && data.has(key)) {
            try {
                JsonElement value = data.get(key);
                if (value.isJsonNull()) {
                    return null;
                }
                return value.getAsInt();
            } catch (Exception e) {
                throwParamTypeError(key, "Integer");
            }
        }
        return null;
    }

    public Short getShort(String key) {
        if(key == null){
            return null;
        }
        if (data != null && data.has(key)) {
            try {
                JsonElement value = data.get(key);
                if (value.isJsonNull()) {
                    return null;
                }
                return value.getAsShort();
            } catch (Exception e) {
                throwParamTypeError(key, "Short");
            }
        }
        return null;
    }

    public Boolean getBoolean(String key) {
        if(key == null){
            return null;
        }
        if (data != null && data.has(key)) {
            try {
                JsonElement value = data.get(key);
                if (value.isJsonNull()) {
                    return null;
                }
                return value.getAsBoolean();
            } catch (Exception e) {
                throwParamTypeError(key, "Boolean");
            }
        }
        return null;
    }

    public Boolean getBoolean(String key, Boolean dValue) {
        Boolean value = getBoolean(key);
        if (null == value) {
            return dValue;
        } else {
            return value;
        }
    }

    public JsonArray getArray(String key) {
        if(key == null){
            return null;
        }
        if (data != null && data.has(key)) {
            try {
                JsonElement value = data.get(key);
                if (value.isJsonNull()) {
                    return null;
                }
                return value.getAsJsonArray();
            } catch (Exception e) {
                throwParamTypeError(key, "JsonArray");
            }
        }
        return null;
    }

    public List<Integer> getIntegerArray(String key) {
        List<Integer> list = new ArrayList<Integer>();
        JsonArray jarray = getArray(key);
        if(jarray != null){
            for(int i=0,size=jarray.size(); i<size; i++) {
                list.add(jarray.get(i).getAsInt());
            }
        }
        return list;
    }

    public List<Long> getLongArray(String key) {
        List<Long> list = new ArrayList<Long>();
        JsonArray jarray = getArray(key);
        if(jarray != null){
            for(int i=0,size=jarray.size(); i<size; i++) {
                list.add(jarray.get(i).getAsLong());
            }
        }
        return list;
    }

    public List<String> getStringArray(String key) {
        List<String> list = new ArrayList<String>();
        JsonArray jarray = getArray(key);
        if(jarray != null){
            for(int i=0,size=jarray.size(); i<size; i++) {
                list.add(jarray.get(i).getAsString());
            }
        }
        return list;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("id=");
        sb.append(id);
        sb.append(",service=");
        sb.append(service);
        sb.append(",encrypt=");
        sb.append(encrypt);
        sb.append(",sign=");
        sb.append(sign);
        if (client != null) {
            sb.append(",client.caller=");
            sb.append(client.caller);
            sb.append(",client.ex");
            sb.append(client.ex);
        }
        if (data != null) {
            sb.append(",data=");
            sb.append(data);
        }

        return sb.toString();
    }
}
